import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from '@/layout'

import dashboardRoutes from './modules/dashboard'
import myselfRoutes from './modules/myself'
import userRoutes from './modules/user'
import deviceRoutes from './modules/device'
import insuranceRoutes from './modules/insurance'
import divideDeviceRoutes from './modules/divideDevice'
import orderRoutes from './modules/order'
import statisticsRoutes from './modules/statistics'
import transationRoutes from './modules/transation'
import activityRoutes from './modules/activity'
import timeCardRoutes from './modules/timeCard'
import systemRoutes from './modules/system'
import alarmRoutes from './modules/alarm'
import messageRoutes from './modules/message'
import permissionRoutes from './modules/permission'
import configRoutes from './modules/config'
import logRoutes from './modules/log'
import appRoutes from './modules/app' //app跳转路由，数组末位
import splitRoutes from './modules/split' //分润路由，数组末位
import accountRoutes from './modules/account' //

/* 权限路由：roles: SUPER_MGR: 超级管理员,AGENT：代理商，OPERATORS:运营商 ，PARTNER:合作商,DEMO:演示 */

Vue.use(VueRouter)

export const constantRoutes = [
  {
    path: '/welcome',
    name: 'Welcome',
    component: Layout,
    meta: { title: '欢迎界面', affix: true, not_cache: true },
    hidden: true,
    children: [
      {
        path: '/welcome',
        component: () => import('@/views/common/welcome'),
        meta: { not_cache: true },
      },
    ],
  },
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path*',
        component: () => import('@/views/redirect/index'),
      },
    ],
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/login'),
    hidden: true,
    meta: {
      title: '登录',
    },
  },
  {
    path: '/agentApply',
    name: 'AgentApply',
    component: () => import('@/views/agentApply'),
    hidden: true,
    meta: {
      title: '申请账号',
    },
  },
  // {
  //   path: '/welcome',
  //   name: 'Welcome',
  //   meta: { title: '欢迎界面', affix: true },
  //   children: [
  //     {
  //       path: '/welcome',
  //       component: () => import('@/views/common/welcome'),
  //     },
  //   ],
  //   hidden: true,
  // },
]

export const asyncRoutes = [
  ...dashboardRoutes,
  ...myselfRoutes,
  ...userRoutes,
  ...deviceRoutes,
  ...divideDeviceRoutes,
  ...orderRoutes,
  ...statisticsRoutes,
  ...transationRoutes,
  ...activityRoutes,
  ...timeCardRoutes,
  ...systemRoutes,
  ...alarmRoutes,
  ...messageRoutes,
  ...configRoutes,
  ...logRoutes,
  ...permissionRoutes,
  ...appRoutes,
  ...insuranceRoutes,
  ...splitRoutes,
  ...accountRoutes,
]
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject)
    return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch((err) => err)
}
export const routes = [...constantRoutes, ...asyncRoutes]
const createRouter = () =>
  new VueRouter({
    routes: routes,
  })

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher
}

const router = createRouter()
export default router
